﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using SVN.App.DataTransferObjects;

namespace SVN.App.DataAccessLayer
{
    abstract public class BasicDAO<T>
    {

        //public abstract bool deleteByID(T ip_obj);

        // connection string
        private static string m_str_connString = (string)ConfigurationSettings.AppSettings["ConnectionString"];
        static SqlConnection m_conn;

        protected virtual DataTable exeSpSelect(string ip_storedProcedure)
        {
            m_conn = BasicDAO<BasicObject>.GetConnection();
            SqlCommand v_command = new SqlCommand(ip_storedProcedure, m_conn);
            SqlDataAdapter adapter = new SqlDataAdapter(v_command);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            m_conn.Close();
            return dt;
        }

        private bool exeSpDeleteById(string ip_storedProcedure, int ip_id)
        {
            m_conn = BasicDAO<BasicObject>.GetConnection();
            SqlCommand v_command = new SqlCommand(ip_storedProcedure, m_conn);
            v_command.CommandType = CommandType.StoredProcedure;
            v_command.Parameters.Add("@Id", SqlDbType.Int).Value = ip_id;

            //m_conn.Open();
            int v_rows = v_command.ExecuteNonQuery();
            m_conn.Close();

            if (v_rows > -1)
                return true;
            else
                return false;
        }
        protected DataTable exeSpSelectById(string ip_storeProcedure, int ip_id)
        {
            DataTable op_dt = new DataTable();

            m_conn = BasicDAO<BasicObject>.GetConnection();
            SqlCommand v_comm = new SqlCommand(ip_storeProcedure, m_conn);
            v_comm.CommandType = CommandType.StoredProcedure;
            v_comm.Parameters.Add("@Id", SqlDbType.Int).Value = ip_id;
            SqlDataAdapter adapter = new SqlDataAdapter(v_comm);
            adapter.Fill(op_dt);
            m_conn.Close();
            return op_dt;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ip_storeProcedure"></param>
        /// <param name="ip_id"></param>
        /// <param name="ip_str_paraName">ten cua tham soo, vi du: @NewsId</param>
        /// <returns></returns>
        protected DataTable exeSpSelectByIdByparaName(string ip_storeProcedure, int ip_id,string ip_str_paraName)
        {
            DataTable op_dt = new DataTable();

            m_conn = BasicDAO<BasicObject>.GetConnection();
            if (m_conn.State == ConnectionState.Closed)
                m_conn.Open();
            SqlCommand v_comm = new SqlCommand(ip_storeProcedure, m_conn);
            v_comm.CommandType = CommandType.StoredProcedure;
            v_comm.Parameters.Add(ip_str_paraName, SqlDbType.Int).Value = ip_id;
            SqlDataAdapter adapter = new SqlDataAdapter(v_comm);
            adapter.Fill(op_dt);
            m_conn.Close();
            return op_dt;
        }
       
        #region Interface

        public static SqlConnection GetConnection()
        {
            SqlConnection v_sql_conn = new SqlConnection(m_str_connString);
            if (v_sql_conn.State == ConnectionState.Closed)
                v_sql_conn.Open();
            return v_sql_conn;
        }

        /// <summary>
        /// Delete 1 record theo Id 
        /// </summary>
        /// <param name="ip_obj">Đối tượng để phân biệt (trong đối tượng chỉ cần thông tin về Id, các trường khác là không cần thiết)</param>
        /// <returns>true: nếu delete thành công, false: nếu xảy ra lỗi</returns>
        public virtual bool deleteByID(BasicObject ip_obj)
        {
            switch (ip_obj.GetType().Name)
            {
                case "":
                    return this.exeSpDeleteById("", ip_obj.Id);
            }
            return false;
        }

        public virtual DataTable selectAll2Dt(T ip_obj)
        {
            switch (ip_obj.GetType().Name)
            {
                case "":
                    return this.exeSpSelect("");
            }

            return null;
        }

        public abstract List<T> selectAll2List(T ip_obj);

        /// <summary>
        /// Lấy ra 1 record dưới dạng bảng 
        /// </summary>
        /// <param name="ip_obj">Đối tượng để phân biệt (trong đối tượng chỉ cần thông tin về Id, các trường khác là không cần thiết)</param>
        /// <returns>DataTable với nhiều nhất 1 record</returns>
        public virtual DataTable selectById2Dt(BasicObject ip_obj)
        {
            switch (ip_obj.GetType().Name)
            {
                case "":
                    return this.exeSpSelectById("", ip_obj.Id);
            }
            return null;
        }

        public abstract bool update(T ip_obj);

        public abstract bool insert(T ip_obj);

        /// <summary>
        /// Cho phep cat di cac the html trong mot string
        /// </summary>
        /// <param name="html"></param>
        /// <param name="allowHarmlessTags"></param>
        /// <returns></returns>
        public virtual string StripHtml(string html, bool allowHarmlessTags)
        {
            if (html == null || html == string.Empty)
                return string.Empty;

            if (allowHarmlessTags)
                return System.Text.RegularExpressions.Regex.Replace(html, "", string.Empty);

            return System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*>", string.Empty);
        }

        #endregion
    }
}
